import Vue from 'vue'
import Router from 'vue-router'
import collateRouter from '@/utils/collateRouter'
import agriculturalSupplies from './module/agricultural-supplies'
import breedAquaticsPopular from './module/breed-aquatics-popular'
import financeInsurance from './module/finance-insurance'
import industryInformation from './module/industry-information'
import specialistConsultation from './module/specialist-consultation'
import systemManagement from './module/system-management'
Vue.use(Router)
import Layout from '@/layout'
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/user-login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/login', //重定向到  
  },
  { path: '*', redirect: '/404', hidden: true }
]
const moduleRouter = [
  agriculturalSupplies,
  breedAquaticsPopular,
  financeInsurance,
  industryInformation,
  specialistConsultation,   // 专家问诊
  systemManagement,
]
console.log(moduleRouter)
//动态路由
moduleRouter.forEach(item => {
  item.children = routerChildren(item.children)
  item.component = routerCom(item.component)
  constantRoutes.push(item)
})

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
const router = createRouter()

export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export function routerCom (path) { //对路由的component解析
  if (path == 'Layout') {
    return (resolve) => require([`@/layout`], resolve);
  } else {
    return (resolve) => require([`@/views${path}`], resolve);
  }
}
export function routerChildren (children) { //对子路由的component解析
  children && children.length > 0 && children.forEach(v => {
    v.component = routerCom(v.component);
    if (v.children && v.children.length > 0) {
      v.children = routerChildren(v.children)
    }
  })
  return children
}
export default router
